﻿using System;
using System.Data;
using Soveryn.Commerce.API;
using Soveryn.Commerce.Shared;

/// <summary>
/// This is the base class for item list doodads.  It retrieves and interprets data from the parent PageBaseClass and decides how to sort and order the list of items.
/// </summary>

namespace Soveryn.Commerce.BaseClasses
{


    public class ItemListBaseClass : DoodadBaseClass
    {

        public DataSet items = new DataSet();

        new protected void Page_Load(object sender, EventArgs e)
        {

            base.Page_Load(sender, e);
            
            DataRelation relation;
            String itemRowFilter = "";
            DataTable itemsDataTable = null;
            DataTable skusDataTable = null;

            //get and sort item list depending on the value of the ItemListType doodad property
            switch (GetPropertyValue("ItemListType"))
            {
                
                case "RelatedItemList":
                    
                    if (!DataUtils.HasRecords(ParentPage.ItemsRelatedDataTable)) return;
                    if (!DataUtils.HasRecords(ParentPage.SKUsRelatedDataTable)) return;

                    items.Tables.Add(ParentPage.ItemsRelatedDataTable.Copy());
                    items.Tables.Add(ParentPage.SKUsRelatedDataTable.Copy());
                    itemsDataTable = items.Tables[0];
                    if (DataUtils.HasRecords(items, 1))
                        skusDataTable = items.Tables[1];

                    itemsDataTable.Columns.Add("ItemPriorityGeneralSort", typeof(decimal));
                    DataUtils.CopyColumns(ref itemsDataTable, "ItemPriorityGeneral", "ItemPriorityGeneralSort");

                    itemsDataTable.DefaultView.Sort = "ItemPriorityGeneralSort";

                    break;

                case "CategoryItemList":
                    items = WebsiteData.ItemListForPageID(ParentPage.PageID, ParentMasterPage.CategoryIDTrail);
                    
                    if (!DataUtils.HasRecords(items, 0)) return;
                    
                    itemsDataTable = items.Tables[0];
                    if (DataUtils.HasRecords(items, 1))
                        skusDataTable = items.Tables[1];

                    itemsDataTable.Columns.Add("ItemPriorityGeneralSort", typeof(decimal));
                    DataUtils.CopyColumns(ref itemsDataTable, "ItemPriorityGeneral", "ItemPriorityGeneralSort");

                    itemsDataTable.DefaultView.Sort = "ItemPriorityGeneralSort";

                    break;

                case "CategorySpecificItemList":
                    items = WebsiteData.ItemListForCategoryID(GetPropertyValue("CategoryID"));

                    if (!DataUtils.HasRecords(items, 0)) return;

                    itemsDataTable = items.Tables[0];
                    if (DataUtils.HasRecords(items, 1))
                        skusDataTable = items.Tables[1];

                    itemsDataTable.Columns.Add("ItemPriorityGeneralSort", typeof(decimal));
                    DataUtils.CopyColumns(ref itemsDataTable, "ItemPriorityGeneral", "ItemPriorityGeneralSort");

                    itemsDataTable.DefaultView.Sort = "ItemPriorityGeneralSort";

                    break;

                case "CategoryItemFeaturedList":

                    items = WebsiteData.ItemListForPageID(ParentPage.PageID, ParentMasterPage.CategoryIDTrail);
                    if (!DataUtils.HasRecords(items, 0)) return;

                    itemsDataTable = items.Tables[0];

                    if (DataUtils.HasRecords(items, 1))
                        skusDataTable = items.Tables[1];

                    if (!itemsDataTable.Columns.Contains("ItemPriorityCategoryFeatured"))
                        itemsDataTable.Columns.Add("ItemPriorityCategoryFeatured", typeof(decimal));

                    itemsDataTable.Columns.Add("ItemPriorityCategoryFeaturedSort", typeof(decimal));
                    DataUtils.CopyColumns(ref itemsDataTable, "ItemPriorityCategoryFeatured", "ItemPriorityCategoryFeaturedSort");


                    itemRowFilter += " and ItemPriorityCategoryFeaturedSort > 0.0";
                    itemsDataTable.DefaultView.Sort = "ItemPriorityCategoryFeaturedSort";

                    break;

                case "GlobalItemFeaturedList":
                    items = WebsiteData.ItemListOfGlobalFeatured();
                    if (!DataUtils.HasRecords(items, 0)) return;

                    itemsDataTable = items.Tables[0];

                    if(DataUtils.HasRecords(items, 1))
                        skusDataTable = items.Tables[1];

                    if (!itemsDataTable.Columns.Contains("ItemPriorityGlobalFeatured"))
                        itemsDataTable.Columns.Add("ItemPriorityGlobalFeatured", typeof(decimal));

                    itemsDataTable.Columns.Add("ItemPriorityGlobalFeaturedSort", typeof(decimal));
                    DataUtils.CopyColumns(ref itemsDataTable, "ItemPriorityGlobalFeatured", "ItemPriorityGlobalFeaturedSort");

                    itemsDataTable.DefaultView.Sort = "ItemPriorityGlobalFeaturedSort ASC";

                    break;

                case "ItemSearchResultList":
                    items = WebsiteData.ItemSearch_nocache(Request.QueryString["Search"], null, false, true, true);
                    if (!DataUtils.HasRecords(items, 0)) return;

                    itemsDataTable = items.Tables[0];
                    if (DataUtils.HasRecords(items, 1))
                        skusDataTable = items.Tables[1];

                    break;

            }

            if (skusDataTable != null)
            {
                relation = new DataRelation("ItemSKU", itemsDataTable.Columns["ItemID"], skusDataTable.Columns["ItemID"], true);
                items.Relations.Add(relation);

                itemsDataTable.DefaultView.RowFilter = "ItemDisplayStatus = " + ((int)Enums.DisplayRights.Everyone).ToString() + itemRowFilter;
                skusDataTable.DefaultView.RowFilter = "SKUDisplayStatus = " + ((int)Enums.DisplayRights.Everyone).ToString();
            }

        }

    }
}
